(function (window) {

    var defaultHostValue = 'https://xyyl.umdemo.cn';
    var constHostKey = 'constHostKey', constTokenKey = 'constTokenKey';
    var click_times = 0;

    window.jsFn = {
        frameName: 'pageContent',
        backClick() {
            api.historyBack({frameName: 'pageContent'}, function (ret) {
                !ret.status && api.closeWin();
            });
        },
        toast(msg) {
            api.toast({msg: msg || 'OK'});
        },
        alert(msg) {
            api.alert({
                title: '提示',
                msg  : msg
            }, function (ret, err) {

            });
        },
        resHost(value) {
            if (value) {
                return api.setPrefs({key: constHostKey, value: value});
            } else {
                return api.getPrefs({sync: true, key: constHostKey}) || defaultHostValue;
            }
        },
        getToken() {
            return api.getPrefs({sync: true, key: constTokenKey}) || '--';
        },
        openDebug() {
            setTimeout(function () {
                click_times > 0 && click_times--;
            }, 1000);
            click_times++;
            if (click_times >= 5) {
                click_times = 0;
                jsFn.toast('打开Debug');
                jsApi.openWindow('Debug', '../html/debug/index.html');
            }
        },
        isOnline() {
            var type = api.connectionType;
            return !(type === 'none' || type === 'unknown');
        },
        openOffline() {
            api.openWin({
                name           : 'Offline',
                url            : '../html/offline.html',
                slidBackEnabled: false
            });
        },
        rebootApp() {
            api.rebootApp();
        }
    };

    window.jsApi = {
        closeWindow() {
            api.closeWin();
        },
        closeToWindow(name) {
            api.closeToWin({
                name: name || 'root'
            });
        },
        openWindow(title, url, name, header) {
            api.openWin({
                name     : name || url,
                url      : '../html/window.html',
                pageParam: {title: title, url: url, header: header}
            });
        },
        openScanWindow(url, header) {
            api.openWin({
                name     : 'scanResult',
                url      : '../html/window.html',
                pageParam: {title: '', url: url, header: header, noBounces: true}
            });
        },
        setToken(value) {
            if (value) {
                return api.setPrefs({key: constTokenKey, value: value});
            } else {
                return api.removePrefs({key: constTokenKey});
            }
        },
        setFrameBounces(bool) {
            api.setFrameAttr({
                name   : jsFn.frameName,
                bounces: !!bool
            });
        },
        saveMediaToAlbum(url, type) {
            type = type || 'jpg';
            var filePath = 'fs://image/' + new Date().getTime() + '.' + type;
            api.download({url: url, savePath: filePath}, function (ret, err) {
                if (ret.state === 1) {
                    api.saveMediaToAlbum({path: filePath}, function (ret, err) {
                        if (ret && ret.status) {
                            jsFn.toast('保存成功');
                        } else {
                            jsFn.toast('保存失败');
                        }
                        var fs = api.require('fs');
                        fs.remove({path: filePath}, function (ret, err) {
                        });
                    });
                } else if (ret.state === 2) {
                    jsFn.toast('下载失败');
                }
            });
        },
        openScan(userid, token) {

            if (!userid || !token) {
                jsFn.toast('参数错误，无法打开扫码');
                return;
            }

            var header = {userid: userid, token: token};

            api.openWin({
                name     : 'scan',
                url      : '../html/scan.html',
                pageParam: {header: header},
                animation: {type: 'fade'}
            });

        },
        openPhotos(images, index) {

            var photoBrowser = api.require('photoBrowser');

            photoBrowser.open({
                images     : typeof images === 'string' ? JSON.parse(images) : images,
                activeIndex: index || 0,
                bgColor    : '#000'
            }, function (ret) {
                if (ret && ret.eventType === 'click')
                    photoBrowser.close();
            });

        }

    };

})(window);